package com.classic.okhttp.gbb.http;

import android.app.Activity;
import android.text.TextUtils;

import com.classic.okhttp.base.OkHttpUtils;
import com.classic.okhttp.base.callback.Callback;
import com.classic.okhttp.gbb.utils.Base64;
import com.classic.okhttp.gbb.utils.MD5;

import java.util.HashMap;
import java.util.Map;

/**
 * 使用步骤： <br />
 * 1.在application初始化: <br />
 * ActionHelp.init(imei,psdnIp, true); <br />
 *
 * @author Jayden
 */
public class ActionHelp {

    private static final String URL_R = "http://112.74.133.33:8015/app/patriarchDeal.do"; //商用
    private static final String URL_T = "http://120.25.159.74:8015/app/patriarchDeal.do"; //商测
    //    private static final String URL_T = "http://192.168.1.40:8015/gbb/app/patriarchDeal.do"; //商测
    public static final String CONTENTTYPE = "application/xml;charset=UTF-8";
    public static final String UPLOAD_CONTENTTYPE = "application/octet-stream";
    private static final String CHARTSET = "UTF-8";
    private static String ACCOUNTTYPE = "1";                                      //1手机账号  2 邮箱
    private static String CLIENTTYPE = "android_2.0.0";
    private static String CLIENT_IMEI;
    private static String USER_IP;
    private static String ACCOUNT;
    private static String URL;
    private static boolean ISRELEASE;

    /**
     * 接口调用前，需要调用此方法
     *
     * @param imei
     * @param psdnIp
     * @param isRelease 配置服务环境 true:商用，false:商测
     */
    public static void init(String imei, String psdnIp, boolean isRelease) {
        CLIENT_IMEI = TextUtils.isEmpty(imei) ? "" : imei;
        USER_IP = TextUtils.isEmpty(psdnIp) ? "" : psdnIp;
        ISRELEASE = isRelease;
        URL = isRelease ? URL_R : URL_T;
    }


    /**
     * 获取帐号类型 1：手机账号, 2: 邮箱
     */
    public static String getAccountType() {
        return ACCOUNTTYPE;
    }

    /**
     * 设置账号类型 1：手机账号,2: 邮箱
     */
    public static void setAccountType(String accountType) {
        ACCOUNTTYPE = accountType;
    }

    /**
     * 获取客户端类型
     */
    public static String getClientType() {
        return CLIENTTYPE;
    }

    /**
     * 设置客户端类型
     */
    public static void setClientType(String clientType) {
        CLIENTTYPE = clientType;
    }

    /**
     * 获取当前帐号
     */
    public static String getAccount() {
        return ACCOUNT;
    }

    /**
     * 设置账号
     */
    public static void setAccount(String account) {
        ACCOUNT = account;
    }

    /**
     * 获取接口url
     *
     * @return
     */
    public static String getUrl() {
        return URL;
    }

    /**
     * 获取登录key
     */
//    public static String getLoginKey() {
//        return LOGIN_KEY;
//    }

    /**
     * 设置登录key(登录成功时调用)
     *
     * @param key
     */
//    public static void setLoginKey(String key) {
//        if (!TextUtils.isEmpty(key)) LOGIN_KEY = key;
//    }

    /**
     * 设置默认的登录key(切换账号、注销时调用)
     */
//    public static void setDefaultLoginKey() {
//        LOGIN_KEY = "LQSW@)!$";
//    }

    /**   ---------------------------------------------------------------------用户管理start-------------------------------------------------------------------------    */


    /**
     * 登录
     *
     * @param activity
     * @param account  账号
     * @param password 密码
     * @param callback
     */
    public static void userLogin(Activity activity, String account, String password, Callback callback) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("username", account);
        params.put("password", password);
//        params.put("password", MD5.MD5Encode(password));

        OkHttpUtils.post()
                .url(getUrl("?cmdType=PATRIARCH_LOGIN"))
                .tag(activity)
                .params(params)
                .build()
                .execute(callback);
    }

    /**
     * 退出登录
     *
     * @param activity
     * @param callback
     */

    public static void userLoginOut(Activity activity, Callback callback) {
        OkHttpUtils.post()
                .url(getUrl("?cmdType=PATRIARCH_LOGINOUT"))
                .tag(activity)
                .build()
                .execute(callback);
    }

    /**
     * 获取验证码
     *
     * @param activity
     * @param phone
     * @param callback
     */

    public static void gbbVerifyCode(Activity activity, String phone, Callback callback) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("phone", phone);

        OkHttpUtils.post()
                .url(getUrl("?cmdType=PATRIARCH_VERIFYCODE"))
                .tag(activity)
                .params(params)
                .build()
                .execute(callback);
    }

    /**
     * 找回密码
     *
     * @param activity
     * @param phone
     * @param password
     * @param code
     * @param callback
     */
    public static void gbbFindPassword(Activity activity, String phone, String password, String code, Callback callback) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("phone", phone);
        params.put("password", password);
        params.put("code", code);

        OkHttpUtils.post()
                .url(getUrl("?cmdType=PATRIARCH_FINDPASSWORD"))
                .tag(activity)
                .params(params)
                .build()
                .execute(callback);
    }

    /**
     * 上传头像
     *
     * @param activity
     * @param imagefile
     * @param callback
     */
    public static void gbbUploadHeadImg(Activity activity, String imagefile, Callback callback) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("imagefile", imagefile);

        OkHttpUtils.post()
                .url(getUrl("?cmdType=PATRIARCH_UPLOADHEADIMG"))
                .tag(activity)
                .params(params)
                .build()
                .execute(callback);
    }

    /**
     * 查询基本信息
     *
     * @param activity
     * @param callback
     */

    public static void gbbQueryUserInfo(Activity activity, Callback callback) {
        OkHttpUtils.post()
                .url(getUrl("?cmdType=PATRIARCH_QUERYUSERINFO"))
                .tag(activity)
                .build()
                .execute(callback);
    }


    /**
     * 设置基本信息
     *
     * @param activity
     * @param nickname
     * @param birthday
     * @param sex
     * @param height
     * @param weight
     * @param callback
     */
    public static void gbbUserInfo(Activity activity, String nickname, String birthday, int sex, int height, double weight, int sport, Callback callback) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("nickname", nickname);
        params.put("birthday", birthday);
        params.put("sex", sex + "");
        params.put("height", height + "");
        params.put("weight", weight + "");
        params.put("sobject", sport + "");

        OkHttpUtils.post()
                .url(getUrl("?cmdType=PATRIARCH_ADDUSERINFO"))
                .tag(activity)
                .params(params)
                .build()
                .execute(callback);
    }

    /**
     * 设置运动目标
     *
     * @param activity
     * @param sobject
     * @param callback
     */

    public static void gbbSportsSetting(Activity activity, int sobject, Callback callback) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("sobject", sobject + "");

        OkHttpUtils.post()
                .url(getUrl("?cmdType=PATRIARCH_SPORTSSETSAVE"))
                .tag(activity)
                .params(params)
                .build()
                .execute(callback);
    }

    /**
     * 获取电量
     *
     * @param activity
     * @param callback
     */
    public static void gbbSelectQty(Activity activity, Callback callback) {
        OkHttpUtils.post()
                .url(getUrl("?cmdType=PATRIARCH_SELECTQTY"))
                .tag(activity)
                .build()
                .execute(callback);
    }


    /**   ---------------------------------------------------------------------用户管理end-------------------------------------------------------------------------    */


    /**  ---------------------------------------------------------------------家校互通start-------------------------------------------------------------------------    */


    /**
     * 家校互通消息
     *
     * @param activity
     * @param pageNum
     * @param callback
     */
    public static void gbbMessages(Activity activity, int pageNum, Callback callback) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("pageNum", pageNum + "");

        OkHttpUtils.post()
                .url(getUrl("?cmdType=PATRIARCH_MESSAGES"))
                .tag(activity)
                .params(params)
                .build()
                .execute(callback);
    }

    /**
     * 发布家校互通消息
     *
     * @param activity
     * @param iscontentpic
     * @param content
     * @param imagefile
     * @param callback
     */
    public static void gbbPubMsg(Activity activity, int iscontentpic, String content, String imagefile, Callback callback) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("iscontentpic", iscontentpic + "");
        params.put("content", content);
        params.put("imagefile", imagefile);

        OkHttpUtils.post()
                .url(getUrl("?cmdType=PATRIARCH_PUBMSG"))
                .tag(activity)
                .params(params)
                .build()
                .execute(callback);
    }


    /**
     * 删除家校互通消息
     *
     * @param activity
     * @param id
     * @param callback
     */
    public static void gbbDelMsg(Activity activity, int id, Callback callback) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("id", id + "");

        OkHttpUtils.post()
                .url(getUrl("?cmdType=PATRIARCH_DELMSG"))
                .tag(activity)
                .params(params)
                .build()
                .execute(callback);
    }

    /**
     * 家校互通评论
     *
     * @param activity
     * @param id
     * @param commentype
     * @param content
     * @param callback
     */

    public static void gbbPubComment(Activity activity, int id, int commentype, String content, Callback callback) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("id", id + "");
        params.put("commentype", commentype + "");
        params.put("content", content);

        OkHttpUtils.post()
                .url(getUrl("?cmdType=PATRIARCH_PUBCOMMENT"))
                .tag(activity)
                .params(params)
                .build()
                .execute(callback);
    }

    /**
     * 屏蔽消息
     *
     * @param activity
     * @param id
     * @param userid
     * @param callback
     */

    public static void gbbShieidMsg(Activity activity, int id, int userid, Callback callback) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("id", id + "");
        params.put("userid", userid + "");

        OkHttpUtils.post()
                .url(getUrl("?cmdType=PATRIARCH_SHIEIDMSG"))
                .tag(activity)
                .params(params)
                .build()
                .execute(callback);
    }

    /**
     * 解除屏蔽
     *
     * @param activity
     * @param userid
     * @param callback
     */
    public static void gbbDelShieid(Activity activity, int userid, Callback callback) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("userid", userid + "");

        OkHttpUtils.post()
                .url(getUrl("?cmdType=PATRIARCH_DELSHIEID"))
                .tag(activity)
                .params(params)
                .build()
                .execute(callback);
    }

    /** ---------------------------------------------------------------------家校互通end------------------------------------------------------------------------- */


    /** ---------------------------------------------------------------------课程食谱start------------------------------------------------------------------------- */

    /**
     * 查询课程/食谱
     *
     * @param activity
     * @param isRC
     * @param classId
     * @param callback
     */

    public static void gbbQueryCourseOrRecipe(Activity activity, String isRC, int classId, Callback callback) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("isRC", isRC);
        params.put("classId", classId + "");

        OkHttpUtils.post()
                .url(getUrl("?cmdType=PATRIARCH_COURSEORRECIPE"))
                .tag(activity)
                .params(params)
                .build()
                .execute(callback);
    }

    public static void gbbQueryCourseOrRecipe(Activity activity, String isRC, Callback callback) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("isRC", isRC);

        OkHttpUtils.post()
                .url(getUrl("?cmdType=PATRIARCH_COURSEORRECIPE"))
                .tag(activity)
                .params(params)
                .build()
                .execute(callback);
    }


    /** ---------------------------------------------------------------------课程食谱end------------------------------------------------------------------------- */

    /** ---------------------------------------------------------------------考勤管理start------------------------------------------------------------------------- */


    /**
     * 考勤查询
     *
     * @param activity
     * @param startDate
     * @param endDate
     * @param callback
     */
    public static void gbbQueryAttendance(Activity activity, String startDate, String endDate, Callback callback) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("startDate", startDate);
        params.put("endDate", endDate);

        OkHttpUtils.post()
                .url(getUrl("?cmdType=PATRIARCH_ATTENDANCE"))
                .tag(activity)
                .params(params)
                .build()
                .execute(callback);
    }


    /**
     * 请假
     *
     * @param activity
     * @param beginDate
     * @param endDate
     * @param Reason
     * @param callback
     */
    public static void gbbSubmitLeave(Activity activity, String beginDate, String endDate, String Reason, Callback callback) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("beginDate", beginDate);
        params.put("endDate", endDate);
        params.put("Reason", Reason);

        OkHttpUtils.post()
                .url(getUrl("?cmdType=PATRIARCH_LEAVE"))
                .tag(activity)
                .params(params)
                .build()
                .execute(callback);
    }

    /**
     * 获取请假列表
     *
     * @param activity
     * @param callback
     */

    public static void gbbLeaveDetail(Activity activity, Callback callback) {

        OkHttpUtils.post()
                .url(getUrl("?cmdType=PATRIARCH_LEAVEDETAIL"))
                .tag(activity)
                .build()
                .execute(callback);
    }

    /**
     * ---------------------------------------------------------------------考勤管理end-------------------------------------------------------------------------
     */

    /**
     * 获取运动信息
     */
    public static void getSportInfo(Activity activity, Callback callback) {
        Map<String, String> params = new HashMap<String, String>();
      /*  params.put("username", account);
        params.put("password", password);*/
        OkHttpUtils.post()
                .url(getUrl("?cmdType=PATRIARCH_SPORT"))
                .tag(activity)
                .params(params)
                .build()
                .execute(callback);
    }

    /**
     * 获取睡眠信息
     */
    public static void getSleepInfo(Activity activity, Callback callback) {
        Map<String, String> params = new HashMap<String, String>();
      /*  params.put("username", account);
        params.put("password", password);*/
        OkHttpUtils.post()
                .url(getUrl("?cmdType=PATRIARCH_SLEEP"))
                .tag(activity)
                .params(params)
                .build()
                .execute(callback);
    }

    /**
     * 获取body信息
     */
    public static void getBodyInfo(Activity activity, Callback callback) {
        Map<String, String> params = new HashMap<String, String>();
      /*  params.put("username", account);
        params.put("password", password);*/
        OkHttpUtils.post()
                .url(getUrl("?cmdType=PATRIARCH_CHILDBODY"))
                .tag(activity)
                .params(params)
                .build()
                .execute(callback);
    }

    /**
     * 获取消息列表
     *
     * @param activity
     * @param callback
     */
    public static void getMessageInfo(Activity activity, int pageNum, Callback callback) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("pageNum", pageNum + "");
        OkHttpUtils.post()
                .url(getUrl("?cmdType=PATRIARCH_MESSAGEINFORM"))
                .tag(activity)
                .params(params)
                .build()
                .execute(callback);
    }

    /**
     * 修改消息状态
     *
     * @param activity
     * @param id
     * @param callback
     */
    public static void changeMessageState(Activity activity, int id, Callback callback) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("id", id + "");
        OkHttpUtils.post()
                .url(getUrl("?cmdType=PATRIARCH_MESSAGEINFORMSTATUS"))
                .tag(activity)
                .params(params)
                .build()
                .execute(callback);
    }

    /**
     * 获取打卡信息
     *
     * @param activity
     * @param callback
     */

    public static void gbbGetCardMsg(Activity activity, Callback callback) {
        OkHttpUtils.post()
                .url(getUrl("?cmdType=PATRIARCH_PUNCHCLOCKNOTES"))
                .tag(activity)
                .build()
                .execute(callback);
    }

    /**
     * 打卡
     *
     * @param activity
     * @param callback
     */

    public static void gbbCard(Activity activity, Callback callback) {
        OkHttpUtils.post()
                .url(getUrl("?cmdType=PATRIARCH_PUNCHCLOCK"))
                .tag(activity)
                .build()
                .execute(callback);
    }


    private static String getUrl(String action) {
        return new StringBuffer(URL).append(action).toString();
    }


    private static Map<String, String> getHeaders() {
        final HashMap<String, String> headerMap = new HashMap<>();
        headerMap.put("Connection", "Keep-Alive");
        headerMap.put("Chartset", CHARTSET);
        headerMap.put("Cache-Control", "no-cache");
        headerMap.put("Accept-Language", "zh-CN,en-US");
        headerMap.put("client-imei", CLIENT_IMEI);
        headerMap.put("user-ip", USER_IP);
        if (!TextUtils.isEmpty(ACCOUNT)) {
            headerMap.put("phone-number", ACCOUNT);
        }
        return headerMap;
    }

    private static Map<String, String> getHeaders(String account) {
        final HashMap<String, String> headerMap = new HashMap<>();
        headerMap.put("Connection", "Keep-Alive");
        headerMap.put("Chartset", CHARTSET);
        headerMap.put("Cache-Control", "no-cache");
        headerMap.put("Accept-Language", "zh-CN,en-US");
        headerMap.put("client-imei", CLIENT_IMEI);
        headerMap.put("user-ip", USER_IP);
        if (!TextUtils.isEmpty(account)) {
            headerMap.put("phone-number", account);
        }
        return headerMap;
    }
}
